package demo1;

import com.mysql.jdbc.StatementInterceptor;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class Test1 {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).
                setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("0118");

        Connection connection = dataSource.getConnection();

        long start = 0 ;
        long end = 0 ;
        start= System.currentTimeMillis();
        String sql = "insert into test values (?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1 , 1);
        preparedStatement.setString(2 , "张三");
        System.out.println(preparedStatement.executeUpdate());
        end = System.currentTimeMillis();
        System.out.println("耗时"+(end - start));

        start= System.currentTimeMillis();
        String sql2 = "insert into test values(2,'王五')";
        Statement statement = connection.prepareStatement(sql2);
        System.out.println(statement.executeUpdate(sql2));
        end = System.currentTimeMillis();
        System.out.println("耗时"+(end - start));


        statement.close();
        preparedStatement.close();
        connection.close();
        System.out.println();

    }
}
